package org.ym.database.spy;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.ym.tools.DateTimeTool;

import java.util.Date;

/**
 * p6spy 打印格式
 * @author ym
 */
public class P6SpyLogger implements MessageFormattingStrategy {

    public P6SpyLogger() {
    }

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        StringBuilder stringBuilder = new StringBuilder();
        if(StringUtils.isBlank(sql)){
            return "";
        }else{
            return stringBuilder.append("/* ")
                    .append(DateTimeTool.formatDate(new Date(),DateTimeTool.FORMAT_DATE_TIME_FULL))
                    .append(" | took ").append(elapsed)
                    .append("ms | ").append(category)
                    .append(" | connection ")
                    .append(connectionId)
                    .append(" */ \n ")
                    .append(sql)
                    .append(";")
                    .toString();
        }
    }
}
